package com.emm.yixun.jobs;

import com.alibaba.fastjson.JSON;
import com.emm.yixun.common.app.base.CommonResDTO;
import com.emm.yixun.common.enums.ResponseEnums;
import com.emm.yixun.common.model.PmoPush;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.PmoLogDto;
import com.emm.yixun.common.model.dto.PmoPushDto;
import com.emm.yixun.common.model.enums.IsPushEnum;
import com.emm.yixun.common.model.enums.MerchantEnum;
import com.emm.yixun.common.model.pmo.LevelDynamiSalesData;
import com.emm.yixun.common.model.pmo.SalesBackOfYear;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.common.utils.MD5;
import com.emm.yixun.common.utils.RequestUtil;
import com.emm.yixun.estate.service.ProjectServiceFacade;
import com.emm.yixun.jobs.config.PmoConfig;
import com.emm.yixun.system.service.NoticeServiceFacade;
import com.emm.yixun.system.service.PmoLogServiceFacade;
import com.emm.yixun.system.service.PmoPushServiceFacade;
import com.emm.yixun.system.service.pmo.PmoDynamicDataServiceFacade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/***
 * 员工关怀
 * 描述:
 * 	员工生日提醒,提前7天(后台设置)通知销售组所有人
 *  客户生日当天提醒
 *  客户纪念日提醒,提前X天(后台设置)通知经纪人
 *  客户纪念日当天提醒
 */
@Component
public class PmoPushJob {
	private static Logger logger = LoggerFactory.getLogger(PmoPushJob.class);

	@Autowired
	private PmoConfig pmoConfig;
	@Autowired
	private PmoPushServiceFacade pmoPushServiceFacade;
	@Autowired
	private ProjectServiceFacade projectServiceFacade;
	@Autowired
	private PmoLogServiceFacade pmoLogServiceFacade;
	@Autowired
	private PmoDynamicDataServiceFacade pmoDynamicDataServiceFacade;

	 
	@Scheduled(cron="${quartz.cron.pmoPushJob}")
 	public void customerCareNotice(){
		logger.info("推送PMO数据 job start..........");
		PmoPushDto pmoPushDto = new PmoPushDto();
		pmoPushDto.setIsPushed(IsPushEnum.TOPUSH.getNo());
		ResponseList<PmoPush> pmoPushResList = pmoPushServiceFacade.findByDto(pmoPushDto);
	 	if (pmoPushResList.isSuccess()) {
		 	List<PmoPush> pmoPushList = pmoPushResList.getEntity();
		 	if(null != pmoPushList && pmoPushList.size() > 0){
				for(PmoPush pmoPush: pmoPushList){
					sendLevelProjectData(pmoPush.getPmoCode(), pmoPush.getPushMonth(), false);

					//推送后,重置记录推送状态
					BeanUtils.copyProperties(pmoPush, pmoPushDto);
					pmoPushDto.setIsPushed(IsPushEnum.PUSHED.getNo());
					pmoPushServiceFacade.updateByPrimaryKey(pmoPushDto, true);
				}
			}
	 	}
	 	logger.info("推送PMO数据 job end..........");
	 }

	public void sendLevelProjectData(String pmoProjectCode, String month, boolean isAll) {
		try {
			sendData(pmoProjectCode, month,null);
		} catch (Exception e) {
			logger.error("PMO项目Code: " + pmoProjectCode + ", send ProjectData error, Job error:", e);
		}finally {

		}
	}
	//绿城专属
	public void sendData(String pmoProjectCode, String month,List<SalesBackOfYear> salesBackOfYear) throws Exception{

		Response<LevelDynamiSalesData> response = pmoDynamicDataServiceFacade.quereyLevelDynamiSalesData(pmoProjectCode, month);

		//Response<LevelDynamiSalesData> response = projectServiceFacade.quereyLevelDynamiSalesData(pmoProjectCode,month);

		boolean pmoPushFlag = projectServiceFacade.checkPmoPush(pmoProjectCode);
		if(pmoPushFlag){
			response.getEntity().setSalesBackOfYears(salesBackOfYear);
			LevelDynamiSalesData levelDynamiSalesData = response.getEntity();
			String json = JSON.toJSONString(levelDynamiSalesData);
			String clientKey = pmoConfig.getClientKey();
			String seq = pmoConfig.getSeq();
			String digest = MD5.md5(clientKey + seq + pmoConfig.getClientPwd()).toUpperCase();
			String url = pmoConfig.getRequest_url() + "/api/v1/pmo/project-dynamic-sale/" + pmoProjectCode;
			com.emm.yixun.common.utils.ResponseBody responseBody = RequestUtil.buildRequest(json, url, clientKey, seq,digest);
			logger.info("推送PMO数据返回内容:", responseBody);
			PmoLogDto pmoLogDto = new PmoLogDto();
			pmoLogDto.setMerchantId(Long.valueOf(MerchantEnum.GREEN_TOWN.getNo()));
			pmoLogDto.setDirection("1");
			if (responseBody.getCode().equals(ResponseEnums.RESPONSEENUMS_000000.getCode())) {// 成功
				pmoLogDto.setStatus("1");
			} else {
				pmoLogDto.setStatus("2");
			}
			pmoLogDto.setRequestParams(json);
			pmoLogDto.setDelFlag("0");
			pmoLogDto.setInterfaceName("销售动态信息");
			pmoLogDto.setReturnResult(JSON.toJSONString(responseBody));
			pmoLogServiceFacade.save(pmoLogDto);
		}
	}

}
